package com.sam;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MemberController
 */
public class MemberControl extends HttpServlet {
        private static final long serialVersionUID = 1L;
        private static final String LIST_MEMBER = "lst_member.jsp";
        private static final String ADD_MEMBER = "add_member.jsp";
        private static final String EDIT_MEMBER = "edit_member.jsp";

        private MemberDAO dao;

        /**
         * @see HttpServlet#HttpServlet()
         */
        public MemberControl() {
                super();
                dao = new MemberDAO();
        }

        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
         *      response)
         */
        protected void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
                String forward = LIST_MEMBER;
                String action = request.getParameter("action");
                
                System.out.println("action = " + action);
                System.out.println("forward = " + forward);
                
                if (action.equalsIgnoreCase("lst_member")) {
                	forward = LIST_MEMBER;
                }
                        
                // add member
                if (action.equalsIgnoreCase("add_member")) {
                    if (request.getParameter("userid") != null) {
                            // save
                            String userid = request.getParameter("userid");
                            String username = request.getParameter("username");
                            String nickname = request.getParameter("nickname");
                            String password = request.getParameter("password");
                            String email = request.getParameter("email");
                            String status = " ";
                            Member m = new Member(userid, username, nickname, password, email, status);
                            dao.add(m);
                            forward = LIST_MEMBER;
                    } else {
                            forward = ADD_MEMBER;
                    }
                }	
                
                // get all members
                List<Member> members = dao.getAll();
                request.setAttribute("members", members);
                System.out.println("forward = " + forward);
                RequestDispatcher dispatcher = request
                                .getRequestDispatcher(forward);
                dispatcher.forward(request, response);
        }

        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
         *      response)
         */
        protected void doPost(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
        	
        	// edit member
        	String action =  request.getParameter("action");
            if (action.equalsIgnoreCase("edit_member")) {
            	Member m = new Member(request.getParameter("userid"),
            						  request.getParameter("username"),
                                      request.getParameter("nickname"),
                                   	  request.getParameter("password"), 
                                   	  request.getParameter("email"),
                                   	  request.getParameter("status"));
            	dao.update(m);
            	List<Member> members = dao.getAll();
                request.setAttribute("members", members);
                RequestDispatcher dispatcher = request
                                .getRequestDispatcher(LIST_MEMBER);
                dispatcher.forward(request, response);    
            }
            
        }

}